const app = getApp();
const {
  unionAsset
} = require("../../../api/api");
const WXAPI = require("../../../api/newApi");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1,
      // 是否显示左上角图标   1表示显示    0表示不显示
      title: '团购专题',
      // 导航栏 中间的标题
      textColor: '#222',
      // 标题颜色
      bgColor: '#fff',
      // 导航栏背景颜色
      isloading: true,
      isColor: true
    },
    grouponId: '', //团购id
    pageSize: 10,
    pageNum: 1,
    cardTotal: 0,
    lodingtype: 1, //加载状态（0：加载中  1：加载完成没有更多）
    imgHttp: app.globalData.imgHttp, //图片前缀
    shopImgList: [], //轮播图
    navLs: [],
    list: [],
    changeIndex: 0,
    scrolldetail: 'a', //用于scrollView滑动到指定位置
    openShow: false, //是否展开
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBanner()
    this.getConfig()
    this.getConfigureSelect()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  getBanner(){ //获取轮播图
    WXAPI.getBanner('',res => {
      if(res.code == 200){
        this.setData({
          shopImgList: res.result
        })
      }
    })
  },
  jump(e){ //轮播图跳转
    let url = e.currentTarget.dataset.item.redirectUrl
    console.log(url)
    if(url){
      wx.navigateTo({
        url: url,
      })
    }
  },
  getConfig() { //获取专题标题
    WXAPI.getConfig('', res => {
      if (res.code == 200) {
        this.setData({
          'nvabarData.title': res.result
        })
      }
    })
  },
  getConfigureSelect() { //获取 团购专题配置分类
    WXAPI.configureSelect('', res => {
      if (res.code == 200) {
        let list = res.result
        list.forEach((item) => {
          item.scrolldetail = 'a' + item.id
        })
        this.setData({
          navLs: list
        })
        this.data.grouponId = list[0].id
        this.getNavList()
      }
    })
  },
  change(e) { //nav选择
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    this.data.scrolldetail = 'a' + item.id
    this.setData({
      changeIndex: index,
      scrolldetail: this.data.scrolldetail,
      openShow: false,
    })
    this.data.grouponId = item.id
    this.data.pageNum = 1,
    this.getNavList()
  },
  clickOpen() { //展开
    this.data.openShow = !this.data.openShow
    this.setData({
      openShow: this.data.openShow
    })
  },
  clickBuy(e) { //立即抢购
    // let id = '1535101883939676161'
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/userPageC/pages/abulkDetail/abulkDetail?id=' + id,
    })
  },
  getNavList() { //获取团购列表
    wx.showLoading({
      title: '加载中',
    })
    var data = {
      id: this.data.grouponId,
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum
    }
    WXAPI.grouponList(data, (res) => {
      const data = res.result
      if (res.code == 200) {
        // this.data.cardtotal = res.result.total
        if (res.result.total == -1) {
          this.data.cardTotal = 0
        } else {
          this.data.cardTotal = res.result.total
        }
        this.setData({
          cardTotal: this.data.cardTotal
        })
        console.log("判断", this.data.pageNum, Math.ceil(this.data.cardTotal / this.data.pageSize), this.data.cardTotal)
        if (this.data.pageNum >= Math.ceil(this.data.cardTotal / this.data.pageSize)) {
          this.setData({
            lodingtype: 1
          })
        } else {
          this.setData({
            lodingtype: 0
          })
        }
        if (this.data.pageNum > 1) {
          this.data.list = this.data.list.concat(res.result.records); //将数据拼接在一起
          this.setData({
            list: this.data.list
          })
        } else {
          this.data.list = res.result.records
          this.setData({
            list: this.data.list
          })
        }
        wx.hideLoading({})
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("触底了", this.data.pageNum, Math.ceil(this.data.cardTotal / this.data.pageSize), this.data.cardTotal)
    if (this.data.pageNum >= Math.ceil(this.data.cardTotal / this.data.pageSize)) {
      console.log('没有分页')
      this.setData({
        lodingtype: 1
      })
    } else {
      this.setData({
        lodingtype: 0
      })
      this.data.pageNum++; //每触底一次 page + 1;
      this.getNavList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    console.log('this.data.id',this.data.id)
    var id = this.data.id
    return {
      title: this.data.nvabarData.title,
      path: '/userPageC/pages/groupBuyingTopic/groupBuyingTopic',
    }
  }
})